﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Pms.Utils;
using System.Data.OracleClient;
using Biblioteca.DataBase;
using Biblioteca.Utils;

namespace SERIX.Classes
{
    public class CTiposTerritorios
    {

        //variaveis
        private string IDT_TPO_TERRI;
        private string DSC_TPO_TERRI;

        //CONSTRUTORES
        public CTiposTerritorios()
        {

        }

        public CTiposTerritorios(string IDT_TPO_TERRI, string DSC_TPO_TERRI)
        {
            this.IDT_TPO_TERRI = IDT_TPO_TERRI;
            this.DSC_TPO_TERRI = DSC_TPO_TERRI;
        }


        //PROPRIEDADE
        public string Idt_tpo_territorios
        {
            get { return this.IDT_TPO_TERRI; }
            set { this.IDT_TPO_TERRI = value; }
        }

        public string Dsc_tpo_territorios
        {
            get { return this.DSC_TPO_TERRI; }
            set { this.DSC_TPO_TERRI = value; }
        }

        //METODOS
        public void Inserir(string Login)
        {
            OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SRX_TIPOS_TERRITORIOS
			                  ( IDT_TPO_TERRI, DSC_TPO_TERRI )
						   values
						      ( :IDT_TPO_TERRI, :DSC_TPO_TERRI )";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                command.Parameters.Add(new OracleParameter("IDT_TPO_TERRI", OracleType.Number));
                command.Parameters.Add(new OracleParameter("DSC_TPO_TERRI", OracleType.VarChar));

                command.Parameters["IDT_TPO_TERRI"].Value = this.IDT_TPO_TERRI;
                command.Parameters["DSC_TPO_TERRI"].Value = this.DSC_TPO_TERRI;

                command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
        }
        public void Alterar(string Login)
        {
            OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SRX_TIPOS_TERRITORIOS set 
			                   DSC_TPO_TERRI = :DSC_TPO_TERRI
						   where
						       IDT_TPO_TERRI = :IDT_TPO_TERRI";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                command.Parameters.Add(new OracleParameter("IDT_TPO_TERRI", OracleType.Number));
                command.Parameters.Add(new OracleParameter("DSC_TPO_TERRI", OracleType.VarChar));


                command.Parameters["IDT_TPO_TERRI"].Value = this.IDT_TPO_TERRI;
                command.Parameters["DSC_TPO_TERRI"].Value = this.DSC_TPO_TERRI;
                
                command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
        }
        public void Excluir(string Login)
        {
            Tabela tab = new Tabela("SRX_TIPOS_TERRITORIOS", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("IDT_TPO_TERRI", DbType.Int32, this.IDT_TPO_TERRI));
            tab.delete();
        }

        public static DataTable ObterTodos(string login)
        {
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_TIPOS_TERRITORIOS order by DSC_TPO_TERRI ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;

            }
            catch (Exception e)
            {

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
        public static CTiposTerritorios PesquisarPorCodigo(string idt_tpo_terri, string login)
        {
            CTiposTerritorios obj;
            string SQL = @"SELECT *
                            FROM  Serix.SRX_TIPOS_TERRITORIOS 
                            WHERE IDT_TPO_TERRI = :IDT_TPO_TERRI";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
                qy.AdicionarParametro(":IDT_TPO_TERRI", DbType.String, idt_tpo_terri);

                qy.Executar();
                obj = new CTiposTerritorios(qy.ObterCampo("IDT_TPO_TERRI"), qy.ObterCampo("DSC_TPO_TERRI"));
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CTiposTerritorios();
            }
            finally
            {
                qy.FecharConexao();
            }
            return obj;
        }

        public static string ObterIdt()
        {
            string retorno = "";
            OracleConnection conexao = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            string query = @"SELECT COUNT(IDT_TPO_TERRI) FROM SRX_TIPOS_TERRITORIOS";
            OracleCommand cmd = new OracleCommand(query, conexao);

            try
            {
                conexao.Open();

                OracleDataReader read = cmd.ExecuteReader();
                while (read.Read())
                {
                    retorno = read[0].ToString();
                    retorno = (Convert.ToInt32(retorno) + 1).ToString();
                }
                return retorno;
            }
            catch
            {
                return retorno;
            }
            finally { conexao.Close(); }
        }

        
    }
}
